/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managed;

import java.io.Serializable;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import session.BattleBeanLocal;
import session.ConstructBuilderBeanLocal;
import session.GameSessionBeanLocal;
import session.ResourcesProducerBeanLocal;

/**
 *
 * @author antonio, giuseppe, davide
 */
@Named("gameSessionController")
@SessionScoped
public class GameSessionControllerBean implements Serializable {
    
    /*
     * Stateful session bean che rappresenta la sessione di gioco
     */
    @EJB
    private GameSessionBeanLocal gameSessionBean;
    
    @EJB
    private ConstructBuilderBeanLocal constructBuilderBean;
    
    @EJB
    private ResourcesProducerBeanLocal resourcesProducerBean;
    
    @EJB 
    private BattleBeanLocal battleBean;
    
    private String username2;
    private int amount = 0;

    /**
     * Crea una nuova istanza di GameSessionControllerBean
     */
    public GameSessionControllerBean() {
    }

    public String getUsername2() {
        return username2;
    }

    public void setUsername2(String username2) {
        this.username2 = username2;
    }

    public int getAmount() {
        return amount;
    }

    public void setAmount(int amount) {
        this.amount = amount;
    }
    
    public String getPlayerUsername() {
        return gameSessionBean.getPlayer().getUsername();
    }
    
    public void build() {
        boolean result = constructBuilderBean.buildConstruct(gameSessionBean.getPlayer().getUsername(), "House", 1);
        System.out.print("Controller build: " + result);
    }
    
    public void destroy() {
        boolean result = constructBuilderBean.destroyConstruct(gameSessionBean.getPlayer().getUsername(), "House", 1);
        System.out.print("Controller destroy: " + result);
    }
    
    public void produce() {
        boolean result = resourcesProducerBean.produce(gameSessionBean.getPlayer().getUsername(), "House", 1);
        System.out.print("Controller produce: " + result);
    }
    
    public void attack() {
        battleBean.moveBattle(gameSessionBean.getPlayer().getUsername(), amount, username2);
    }
    
    public void find() {
        Collection<String> c = battleBean.findPlayers(gameSessionBean.getPlayer().getUsername());
        System.out.println(c);
    }
    
    public String logout() {
        this.gameSessionBean.remove();
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
        request.getSession().invalidate();
        return "logout";
    }
    
    /*
     * 
     */
    @PostConstruct
    private void init() {
        if(gameSessionBean.getPlayer() == null) {
            try {
                getUserData();
            } catch (ServletException ex) {
                Logger.getLogger(GameSessionControllerBean.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
    private void getUserData() throws ServletException {
        HttpServletRequest request = getRequest();
        gameSessionBean.initPlayer(request.getRemoteUser());
    }
    
    private HttpServletRequest getRequest() throws ServletException {
        ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
        Object requestObject = context.getRequest();
        if(!(requestObject instanceof HttpServletRequest)) {
            Logger.getLogger(GameSessionControllerBean.class.getName()).log(Level.SEVERE, null, "Request object has type: " + requestObject.getClass());
            throw new ServletException();
        }
        HttpServletRequest request = (HttpServletRequest) requestObject;
        return request;
    }
    
}
